package com.xjtu.platform.mainAction;

import com.xjtu.platform.common.PageParam;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.common.page.QueryAction;
import com.xjtu.platform.mainAction.dto.CreateDto;
import com.xjtu.platform.mainAction.dto.UpdateDto;
import com.xjtu.platform.mainAction.entity.Article;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MainServiceImpl implements MainService{

    @Resource
    private MainDao mainDao;

    @Override
    public void createArticle(CreateDto createDto) {
        try {
            Date date = new Date();
            Timestamp nowTime = new Timestamp(date.getTime());
            mainDao.createArticle(createDto,nowTime);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Map<String, Object> getAllArticle(PageParam pageParam) {
        return Utils.getPage(pageParam, new QueryAction<Article>() {
            @Override
            public List<Article> execute() {
                return mainDao.getAllArticle();
            }
        });
    }

    @Override
    public void updateArticle(UpdateDto updateDto) {
        try {
            Date date = new Date();
            Timestamp nowTime = new Timestamp(date.getTime());
            mainDao.updateArticle(updateDto,nowTime);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
